﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using PowerCreator.LMS.Common.Script;

namespace PowerCreator.LMS.Web.Sys.Comment
{
    public partial class ShowComment : System.Web.UI.Page
    {
        PowerCreator.LMS.Web.Comment.Comment commentManager = new PowerCreator.LMS.Web.Comment.Comment();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                string resourceid = Request.QueryString["ResourceID"].ToString();
                ViewState["ResourceID"] = resourceid;

                BinderCommentLevel();

                string search = GetSearch();
                BinderPager(search);
                BinderGV(search,1);
            }
        }
        private string GetSearch()
        {
            string search = string.Format("ResourceID={0}", ViewState["ResourceID"]);
            if (!string.IsNullOrEmpty(txtCommentContent.Text))
            {
                search += string.Format(" and [Title] like '%{0}%' ", txtCommentContent.Text);
            }
            return search;
        }
        private void BinderPager(string search)
        {
            AspNetPager1.RecordCount = commentManager.QueryCommentCount(search);
            AspNetPager1.PageSize = AspNetPager1.RecordCount;
        }
        private void BinderGV(string search, int pageIndex)
        {
            DataTable dt = commentManager.QueryComment(search);
            rptCommentList.DataSource = dt;
            rptCommentList.DataBind();
        }
        private void BinderCommentLevel()
        {
            //List<PowerCreator.LMS.Comment.Logic.CommmentLevelInfo> list = PowerCreator.LMS.Comment.Logic.CommentLevelType.GetCommentLevelList();
            //foreach (PowerCreator.LMS.Comment.Logic.CommmentLevelInfo var in list)
            //{
            //    string search = string.Format("ResourceID={0} and CommentLevel = {1}", ViewState["ResourceID"], var.Key);
            //    var.Count = commentManager.GetCommentCount(search).ToString();
            //    PowerCreator.LMS.Comment.Logic.CommentLevelType.UpdateCommentLevelList(var, list);
            //}
            //rptCommentLevel.DataSource = list;
            //rptCommentLevel.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            string search = GetSearch();            
            BinderGV(search, AspNetPager1.CurrentPageIndex);
        }

        protected void rptCommentList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "mydel")
            {
                int commentid = int.Parse(e.CommandArgument.ToString());
                //PowerCreator.LMS.Comment.CommentInfo.CommentInfo cinfo = commentManager.LoadCommentInfo(commentid);
                //if (cinfo != null)
                //{
                //    int v = commentManager.DelCommentInfo(cinfo);
                //    if (v == 2)
                //    {
                //        PowerCreator.LMS.Logic.Common.Script.Alert(Page, "del", "出现数据库异常!");
                //    }
                //    else if (v == 1)
                //    {
                //        string search = GetSearch();
                //        BinderPager(search);
                //        BinderGV(search, 1);
                //        Script.Alert(Page, "del", "删除成功!");
                //    }
                //    else
                //    {
                //       Script.Alert(Page, "del", "删除数据失败!");
                //    }
                //}
                //else
                //{
                //   Script.Alert(Page, "del", "加载数据为空!");
                //}
            }
            else if (e.CommandName.ToLower() == "mystate")
            {
                int commentid = int.Parse(e.CommandArgument.ToString());
                //PowerCreator.LMS.Comment.CommentInfo.CommentInfo cinfo = commentManager.LoadCommentInfo(commentid);
                //if (cinfo != null)
                //{
                //    //cinfo.StateCount 
                //    int v = commentManager.UpdataComment(cinfo);
                //    if (v == 2)
                //    {
                //        Script.Alert(Page, "update", "更新时出现数据库异常!");
                //    }
                //    else if (v == 1)
                //    {
                //        string search = GetSearch();
                //        BinderPager(search);
                //        BinderGV(search, 1);
                //        Script.Alert(Page, "update", "更新成功!");
                //    }
                //    else
                //    {
                //        Script.Alert(Page, "update", "更新数据失败!");
                //    }
                //}
                //else
                //{
                //    Script.Alert(Page, "update", "更新加载数据为空!");
                //}
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
        }
}
}